package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 游记评论的服务
 */

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentMongoRepository repository;

    /**
     * 保存游记评论的操作
     * 评论信息的处理
     *
     * @param entity
     * @return
     */
    public TravelComment save(TravelComment entity) {
        //对评论信息的处理
        if (TravelComment.TRAVEL_COMMENT_TYPE.equals(entity.getType())) {
            //当前评论为回复评论
            //根据引用评论的id去查询原来评论的信息
            Optional<TravelComment> op = repository.findById(entity.getRefComment().getId());
            if (op.isPresent()) {
                //设置引用对象的信息
                entity.setRefComment(op.get());
            }
        }

        //对游记的评论的保存操作
        entity.setCreateTime(new Date());
        repository.save(entity);
        return entity;
    }

    /**
     * 当前游记的所有评论
     *
     * @param travelId
     * @return
     */
    public List<TravelComment> listByTravelId(Long travelId) {
        return repository.findByTravelId(travelId);
    }

}
